import { cityApi } from "../utils/api";
// 城市
const cityModule = {
  state: {
    cityname: localStorage.getItem("name") || "北京",
    cityid: localStorage.getItem("id") || 110100,
    // 城市选择页
    cityList: [],
  },
  getters: {
    // 热门城市
    hotList(state) {
      return state.cityList[0];
    },
    // 其他城市
    otherList(state) {
      return state.cityList.slice(1);
    },
  },
  mutations: {
    changeCity(state, payload) {
      state.cityname = payload.name;
      state.cityid = payload.cityId;
    },
    // 改变城市列表
    getCity(state, payload) {
      state.cityList = payload.result;
    },
  },
  actions: {
    // 获取城市数据
    async getCityAsync({ commit }) {
      const res = await cityApi();
      // this.cityList = res.result;
      commit("getCity", res);
    },
  },
};

export default cityModule;
